﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterpreterPattern
{
    /// <summary>
    /// 1 解释器模式InterpreterPattern
    /// 2 web开发模板替换的解释器模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训，今天是Eleven老师为大家带来的解释器模式");

                {
                    string teacher = "eleven";
                    Console.WriteLine("转换结果是{0}", WordChange(teacher));
                    Console.WriteLine("转换结果是{0}", WordChangeCommon(teacher));
                }
                {
                    string word = "betry";
                    Console.WriteLine("转换结果是{0}", WordChange(word));
                    Console.WriteLine("转换结果是{0}", WordChangeCommon(word));
                }
                {
                    string word = "elent";
                    //Console.WriteLine("转换结果是{0}", WordChange(word));
                    Console.WriteLine("转换结果是{0}", WordChangeCommon(WordChangeCommon(word)));
                }
                Console.WriteLine("******************");
                {
                    string word = "elevenw84730894389.,/.43278lsaiyfgasulifhnjkasdvnlueqn4piao;";
                    Context context = new Context(word);
                    List<BaseExpression> exprssionList = new List<BaseExpression>();
                    exprssionList.Add(new HighWordExpression());
                    exprssionList.Add(new WordExpression());
                    exprssionList.Add(new SimpleExpression());
                    exprssionList.Add(new LastExpression());//配置解释器

                    foreach (var item in exprssionList)
                    {
                        item.Trans(context);
                    }

                    Console.WriteLine("解释器转换结果是{0}", context.Word);

                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }

        /// <summary>
        /// a b c d e f g h i j 
        /// 1 2 3 4 5 6 7 8 9 0 0 0 0 0
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private static string WordChange(string word)
        {
            if (word.Equals("eleven"))
            {
                return "505050";
            }
            else if (word.Equals("betry"))
            {
                return "25000";
            }
            else
                throw new Exception("wrong word");
        }

        /// <summary>
        /// a b c d e f g h i j k l m n o p q r s t u v w x y z .....
        /// 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p 0
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private static string WordChangeCommon(string word)
        {
            if (string.IsNullOrWhiteSpace(word)) return "";
            List<string> sNumberList = new List<string>();
            foreach (var item in word.ToCharArray())
            {
                switch (item)
                {
                    case 'a':
                        sNumberList.Add("1");
                        break;
                    case 'b':
                        sNumberList.Add("2");
                        break;
                    case 'c':
                        sNumberList.Add("3");
                        break;
                    case 'd':
                        sNumberList.Add("4");
                        break;
                    case 'e':
                        sNumberList.Add("5");
                        break;
                    case 'f':
                        sNumberList.Add("6");
                        break;
                    case 'g':
                        sNumberList.Add("7");
                        break;
                    case 'h':
                        sNumberList.Add("8");
                        break;
                    case 'i':
                        sNumberList.Add("9");
                        break;
                    case 'j':
                        sNumberList.Add("0");
                        break;
                    case 'k':
                        sNumberList.Add("a");
                        break;
                    case 'l':
                        sNumberList.Add("b");
                        break;
                    case 'm':
                        sNumberList.Add("c");
                        break;
                    default:
                        sNumberList.Add("0");
                        break;
                }
            }
            return string.Concat(sNumberList);//把集合中的字符串连接成一个长字符串
        }

    }
}
